package aip.core.dao.user;

import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.codec.binary.Hex;
import org.springframework.dao.DataAccessException;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import aip.core.model.user.User;

import com.googlecode.horncomb.bean.filter.ConditionSet;
import com.googlecode.horncomb.bean.filter.SimpleMapConditionSet;
import com.googlecode.horncomb.bean.order.DefaultOrder;
import com.googlecode.horncomb.bean.order.DefaultOrderToken;
import com.googlecode.horncomb.bean.order.Order;
import com.googlecode.horncomb.bean.paged.PagedList;
import com.googlecode.horncomb.orm.ibatis.SqlMapEntityDaoImpl;

/**
 * User data access object
 * <p>
 * 负责人/principal:<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:<br/>
 * @author
 * @version $Id$
 */
public class UserDao extends SqlMapEntityDaoImpl<User>
{
    private static final long serialVersionUID = 1646395171796311254L;

    public String encodePassword(String password)
    {
        Assert.hasText(password);
        byte[] r;
        try
        {
            r = MessageDigest.getInstance("MD5").digest(
                    password.toUpperCase().getBytes());
            return new String(Hex.encodeHex(r));
        }
        catch (NoSuchAlgorithmException e)
        {
            throw new RuntimeException(e);
        }
    }

    @Override
    public User getById(Serializable id) throws DataAccessException
    {
        return super.getById(id.toString().toUpperCase());
    }

    public User getValidUser(String username)
    {
        Assert.hasText(username);
        return this.doGet(new SimpleMapConditionSet().addConstraint("username",
                username.toUpperCase()).addConstraint("state", 1));
    }

    @Override
    protected void insert(Object entity, String stmtName, String namespace)
            throws DataAccessException
    {
        stmtName = this.buildStatementName(namespace, StringUtils
                .hasText(stmtName) ? stmtName : this.insertStatementName);
        this.getSqlMapClientTemplate().insert(stmtName, entity);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public PagedList<List<? extends User>> query(ConditionSet constraints,
            Order order, int page, int pageSize) throws DataAccessException
    {
        return super.query(constraints, order, page, pageSize);
    }

    public List<User> queryListByIds(String[] ids) throws DataAccessException
    {
        return (List<User>) this.query(new SimpleMapConditionSet()
                .addConstraint("usernames", ids));
    }

    public void updateStateByItems(Set<String> ids, Integer state)
    {
        ConditionSet cons = new SimpleMapConditionSet().addConstraint(
                "usernames", ids.toArray(new String[] {})).addConstraint(
                "state", state).addConstraint("modify", new Date());
        this.doUpdate(cons, null, "updateState");
    }

    public void deleteByUsernames(Set<String> usernames)
    {
        ConditionSet cons = new SimpleMapConditionSet().addConstraint(
                "usernames", usernames.toArray(new String[] {}));
        this.doDelete(cons, null);
    }

    public void deleteByUsername(String username)
    {
        ConditionSet cons = new SimpleMapConditionSet().addConstraint(
                "username", username);
        this.doDelete(cons, null);
    }

    public List<? extends User> queryListByType(String type, String likeDomain,
            int start, int pageSize) throws DataAccessException
    {
        ConditionSet cons = new SimpleMapConditionSet().addConstraint("type",
                type).addConstraint("state", 1).addConstraint("likeDomain",
                likeDomain).addConstraint("joinExpert", true);
        Order order = new DefaultOrder(new DefaultOrderToken("NAME_"));
        return super.query(cons, order, start, pageSize).getSource();
    }

    public int getDataCount(String type, String likeDomain)
    {
        ConditionSet constraints = new SimpleMapConditionSet().addConstraint(
                "type", type).addConstraint("state", 1).addConstraint(
                "likeDomain", likeDomain).addConstraint("joinExpert", true);
        return super.queryCount(constraints);
    }
}
